﻿using System;
using System.Collections.Generic;

using System.Text;
using Portal.Interface;
using Portal.Environment;
using Portal.Language;
using System.Data;
using System.Data.SqlClient;

namespace Portal.CMS
{
    public class LawDocumentItems : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "LawDocumentItems";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Title","Danh sách văn bản tài liệu (items)");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }

        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            try
            {

                AjaxPro.Utility.RegisterTypeForAjax(typeof(LawDocumentItems));
                Environment.Environments.CheckSessionTimeOut(true);

                string LawDocumentId = System.Web.HttpContext.Current.Request["LawDocumentId"];
                LawDocumentCls LawDocumentCls = new LawDocumentBLL().CreateModel(LawDocumentId);
                if (LawDocumentCls == null)
                {
                    return Environment.Environments.GetRecordNotFound();
                }

                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new LawDocument().PermissionTypes, "LawDocumentItems");
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "ShowItems", LawDocumentCls.LawDocumentId, LawDocumentCls.Owner, true);

                string AllSubject = ResourceBLL.GetText(WebPartId, "AllSubject", "== Toàn bộ==");
                DataTable dtLawDocumentOffice = new LawDocumentOfficeBLL().ReadingLawDocumentOffice("");
                string SelectLawDocumentOffice = "<select id=drpSelectLawDocumentOffice>";
                SelectLawDocumentOffice += "<option value=\"\">" + AllSubject + "</option>";
                for (int iIndex = 0; iIndex < dtLawDocumentOffice.Rows.Count; iIndex++)
                {
                    string LawDocumentOfficeId = (string)dtLawDocumentOffice.Rows[iIndex]["LawDocumentOfficeId"];
                    string Subject = (string)dtLawDocumentOffice.Rows[iIndex]["Subject"];
                    SelectLawDocumentOffice += "<option value=\"" + LawDocumentOfficeId + "\">" + Subject + "</option>";
                }
                SelectLawDocumentOffice += "</select>";



                DataTable dtLawDocumentType = new LawDocumentTypeBLL().ReadingLawDocumentType("");
                string SelectLawDocumentType = "<select id=drpSelectLawDocumentType>";
                SelectLawDocumentType += "<option value=\"\">" + AllSubject + "</option>";
                for (int iIndex = 0; iIndex < dtLawDocumentType.Rows.Count; iIndex++)
                {
                    string LawDocumentTypeId = (string)dtLawDocumentType.Rows[iIndex]["LawDocumentTypeId"];
                    string Subject = (string)dtLawDocumentType.Rows[iIndex]["Subject"];
                    SelectLawDocumentType += "<option value=\"" + LawDocumentTypeId + "\">" + Subject + "</option>";
                }
                SelectLawDocumentType += "</select>";

                DataTable dtLawDocumentSigner = new LawDocumentSignerBLL().ReadingLawDocumentSigner("");
                string SelectLawDocumentSigner = "<select id=drpSelectLawDocumentSigner>";
                SelectLawDocumentSigner += "<option value=\"\">" + AllSubject + "</option>";
                for (int iIndex = 0; iIndex < dtLawDocumentSigner.Rows.Count; iIndex++)
                {
                    string LawDocumentSignerId = (string)dtLawDocumentSigner.Rows[iIndex]["LawDocumentSignerId"];
                    string Subject = (string)dtLawDocumentSigner.Rows[iIndex]["Subject"];
                    SelectLawDocumentSigner += "<option value=\"" + LawDocumentSignerId + "\">" + Subject + "</option>";
                }
                SelectLawDocumentSigner += "</select>";

                string RetHtml =
                    WebPartUtilities.AttachJavascript("Portal.CMS", "LawDocument.js") +
                    "<div class=cssHeading>" + LawDocumentCls.Subject + "</div>" +
                    "<div id=divFormSearch>" +
                    "       <table>" +
                    "           <tr>" +
                    "               <td>" + ResourceBLL.GetText(WebPartId, "No", "Số") + ":</td>" +
                    "               <td><input id=txtCode class=cssLawDocumentCode></td>" +
                    "           </tr>" +
                    "           <tr>" +
                    "               <td>" + ResourceBLL.GetText(WebPartId, "Subject", "Tiêu đề") + ":</td>" +
                    "               <td><input id=txtSubject class=cssLawDocumentSubject></td>" +
                    "           </tr>" +
                    "           <tr>" +
                    "               <td>" + ResourceBLL.GetText(WebPartId, "Keyword", "Từ khóa") + ":</td>" +
                    "               <td><input id=txtKeyword class=cssLawDocumentSubject></td>" +
                    "           </tr>" +
                    "           <tr>" +
                    "               <td>" + ResourceBLL.GetText(WebPartId, "SignBy", "Người ký") + ":</td>" +
                    "               <td>" + SelectLawDocumentSigner + "</td>" +
                    "           </tr>" +
                    "           <tr>" +
                    "               <td>" + ResourceBLL.GetText(WebPartId, "Office", "Đơn vị ban hành") + ":</td>" +
                    "               <td>" + SelectLawDocumentOffice + "</td>" +
                    "           </tr>" +
                    "           <tr>" +
                    "               <td>" + ResourceBLL.GetText(WebPartId, "Type", "Loại") + ":</td>" +
                    "               <td>" + SelectLawDocumentType + "</td>" +
                    "           </tr>" +
                    "           <tr>" +
                    "               <td></td>" +
                    "               <td><input onclick=javascript:ClientAddLawDocumentItem('" + LawDocumentId + "'); type=button value=\"" + ResourceBLL.GetText(WebPartId, "AddButton", "Thêm mới") + "\"><input onclick=javascript:ClientSearch('" + LawDocumentId + "'); type=button value=\"" + ResourceBLL.GetText(WebPartId, "Search", "Tìm kiếm") + "\"></td>" +
                    "           </tr>" +
                    "       </table>" +
                    "</div>" +
                    "<div id=divProcessing></div>" +
                    "<div id=divFormResult>" +
                    "</div>";
                return RetHtml;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string Search(string LawDocumentId, string Code, string Subject, string LawDocumentSignerId, string LawDocumentTypeId, string LawDocumentOfficeId,string Keyword, int CurrentPageIndex)
        {
            try
            {
                DataTable dtItems = new LawDocumentBLL().SearchingItems(LawDocumentId, Code, Subject, LawDocumentSignerId, LawDocumentTypeId, LawDocumentOfficeId,Keyword);
                int StartIndex = 0;
                int EndIndex = 0;
                string PagingText = new PagingCls().GetPaging(LawDocumentId, dtItems.Rows.Count, 25, 10, CurrentPageIndex, "JumpPageLawDocumentItem", ref StartIndex, ref EndIndex);

                string Html = "";
                if (dtItems.Rows.Count == 0)
                {
                    Html += "<div class=\"cssNoData\">" + ResourceBLL.GetText(WebPartId, "NoData", "Chưa có dữ liệu nào trong chuyên mục này!") + "</div>";
                }
                else
                {
                    Html +=
                    PagingText +
                         "<input type=hidden id=txtPageIndex value=\"" + CurrentPageIndex + "\">" +
                         "<div class=cssHeading>" + ResourceBLL.GetText(WebPartId, "Total", "Tổng số văn bản") + ": (" + dtItems.Rows.Count.ToString("#,##0") + ")</div>" +
                                   "<table>" +
                                   " <tr>" +
                                   "     <th>#</th>" +
                                   "     <th class=cssTdLawDocumentSubject>" + ResourceBLL.GetText(WebPartId, "Subject", "Tiêu đề") + "</th>" +
                                   "     <th class=cssTdLawDocumentOffice>" + ResourceBLL.GetText(WebPartId, "Office", "Cơ quan") + "</th>" +
                                   "     <th class=cssTdLawDocumentDocType>" + ResourceBLL.GetText(WebPartId, "DocTy pe", "Loại") + "</th>" +
                                   "     <th class=cssTdLawDocumentSigner>" + ResourceBLL.GetText(WebPartId, "Signer", "Người ký") + "</th>" +
                                   "     <th class=cssTdLawDocumentUpdate></th>" +
                                   " </tr>";
                    for (int iIndex = StartIndex; iIndex < EndIndex; iIndex++)
                    {
                        string LawDocumentItemId = (string)dtItems.Rows[iIndex]["LawDocumentItemId"];
                        string tCode = (string)dtItems.Rows[iIndex]["Code"];
                        string tSubject = (string)dtItems.Rows[iIndex]["Subject"];
                        string tOffice = (string)dtItems.Rows[iIndex]["Office"];
                        string tDocType = (string)dtItems.Rows[iIndex]["DocType"];
                        string tSigner = (string)dtItems.Rows[iIndex]["Signer"];

                        string UpdateText = "<a href=\"javascript:UpdateItem('" + LawDocumentItemId + "');\">" + ResourceBLL.GetText(WebPartId, "Update", "(Sửa)") + "</a>";
                        string DeleteText = "<a href=\"javascript:ClientDeleteLawDocumentItem('" + LawDocumentId + "','" + LawDocumentItemId + "');\">" + ResourceBLL.GetText(WebPartId, "Delete", "(Xóa)") + "</a>";
                        string PermissionText = "<a href=\"javascript:PermissionLawDocumentItem('" + LawDocumentItemId + "');\">" + ResourceBLL.GetText(WebPartId, "Permission", "(Quyền)") + "</a>";
                        string UploadText = "<a href=\"javascript:UploadItem('" + LawDocumentItemId + "');\">" + ResourceBLL.GetText(WebPartId, "Upload", "(upload)") + "</a>";
                        string DownloadText = "<a href=\"javascript:DownloadItem('" + LawDocumentItemId + "');\">" + ResourceBLL.GetText(WebPartId, "download", "(download)") + "</a>";
                        string VersionText = "<a href=\"javascript:alert('Chưa làm xong phần này!');\">" + ResourceBLL.GetText(WebPartId, "version", "(version)") + "</a>";
                        string SyncText = "<a href=\"javascript:SyncLawDocumentItem('" + LawDocumentItemId + "');\">" + ResourceBLL.GetText(WebPartId, "SyncText", "(sync)") + "</a>";

                        Html +=
                                  " <tr>" +
                                  "     <td class=cssTdLawDocumentIndex>" + (iIndex + 1) + "</td>" +
                                  "     <td class=cssTdLawDocumentSubject><span class=cssLawCode>" + tCode + "</span><br>" + tSubject + "</td>" +
                                  "     <td class=cssTdLawDocumentOffice>" + tOffice + "</td>" +
                                  "     <td class=cssTdLawDocumentDocType>" + tDocType + "</td>" +
                                  "     <td class=cssTdLawDocumentSigner>" + tSigner + "</td>" +
                                  "     <td class=cssTdLawDocumentUpdate>" + UpdateText + "<br>" + DeleteText + "<br>" + PermissionText + "<br>" + UploadText + "<br>" + DownloadText +"<BR>"+ SyncText +"</td>" +
                                  " </tr>";
                    }
                    Html += "</table>";
                }
                return Html;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetProcessingText()
        {
            string Url = Environment.Environments.GetActiveThemeUrl() + "/Global/Loading.gif";
            return "<img src=\"" + Url + "\"><a class=cssProcessing>" + ResourceBLL.GetText(WebPartId, "ProcessingText", "Đang đọc dữ liệu...") + "</a>";
        }


        [AjaxPro.AjaxMethod]
        public string GetAddLawDocumentItemUrl(string LawDocumentId)
        {
            try
            {
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new LawDocument().PermissionTypes,"LawDocumentItems");
                LawDocumentCls LawDocumentCls = new LawDocumentBLL().CreateModel(LawDocumentId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "AddItem", LawDocumentCls.LawDocumentId, LawDocumentCls.Owner, true);

                string AddLawDocumentItemUrl = Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new AddLawDocumentItem().WebPartId),
                        new ParamCls("LawDocumentId",LawDocumentId)
                    });
                return AddLawDocumentItemUrl;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetUpdateLawDocumentItemUrl(string LawDocumentItemId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new LawDocument().PermissionTypes, "LawDocumentItems");
                LawDocumentItemCls LawDocumentItemCls = new LawDocumentBLL().CreateModelItem(LawDocumentItemId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "UpdateItem", LawDocumentItemId, LawDocumentItemCls.Owner, true);

                string UpdateLawDocumentItemUrl = Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new UpdateLawDocumentItem().WebPartId),
                        new ParamCls("LawDocumentItemId",LawDocumentItemId)
                    });
                return UpdateLawDocumentItemUrl;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetUpdateContentLawDocumentItemUrl(string LawDocumentItemId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new LawDocument().PermissionTypes, "LawDocumentItems");
                LawDocumentItemCls LawDocumentItemCls = new LawDocumentBLL().CreateModelItem(LawDocumentItemId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "UpdateItem", LawDocumentItemId, LawDocumentItemCls.Owner, true);

                string UpdateLawDocumentItemUrl = Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new UploadLawDocumentItem().WebPartId),
                        new ParamCls("LawDocumentItemId",LawDocumentItemId)
                    });
                return UpdateLawDocumentItemUrl;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideDownloadItem(string LawDocumentItemId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                LawDocumentItemCls lawDocumentItemCls = new LawDocumentBLL().CreateModelItem(LawDocumentItemId);

                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new LawDocument().PermissionTypes, "LawDocumentItems");
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "DownloadItem", LawDocumentItemId, lawDocumentItemCls.Owner, true);
           

                byte[] b = Environment.BobController.ReadBlobField(Environments.ConnectionString, "TableLawDocumentItem", "AttachFile", "LawDocumentItemId", LawDocumentItemId);
                if (b == null) throw new Exception(ResourceBLL.GetText(WebPartId, "hasNotAttachedFile", "Không có tài liệu gắn kèm!"));
                
                System.IO.Directory.CreateDirectory(Environments.WorkingPath + "\\temp");
                System.IO.Directory.CreateDirectory(Environments.WorkingPath + "\\temp\\" + Environments.PortalAlias);
                System.IO.Directory.CreateDirectory(Environments.WorkingPath + "\\temp\\" + Environments.PortalAlias+"\\LawDocument");
                string PathSave = Environments.WorkingPath + "\\temp\\" + Environments.PortalAlias + "\\LawDocument\\" + lawDocumentItemCls.RealFile;
                try
                {
                    System.IO.File.Delete(PathSave);
                }
                catch { }
                Environment.BobController.WriteBytesToFile(b, PathSave);
                string FileUrl = Environments.HttpMediaRoot + "/temp/" + Environments.PortalAlias + "/LawDocument/" + lawDocumentItemCls.RealFile;
                return FileUrl;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideCheckDeleteLawDocumentItem(string LawDocumentItemId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                LawDocumentItemCls LawDocumentItemCls = new LawDocumentBLL().CreateModelItem(LawDocumentItemId);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new LawDocument().PermissionTypes, "LawDocumentItems");
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "DeleteItem", LawDocumentItemCls.LawDocumentItemId, LawDocumentItemCls.Owner, true);
                return "";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetConfirmDeleteMsg()
        {
            return ResourceBLL.GetText(WebPartId, "ConfirmDeleteMsg", "Bạn đã chắc chắn xóa chưa?");
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideDeleteLawDocumentItem(string LawDocumentItemId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                LawDocumentItemCls LawDocumentItemCls = new LawDocumentBLL().CreateModelItem(LawDocumentItemId);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new LawDocument().PermissionTypes, "LawDocumentItems");
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "DeleteItem", LawDocumentItemCls.LawDocumentItemId, LawDocumentItemCls.Owner, true);

                new LawDocumentBLL().DeleteLawDocumentItem(null, LawDocumentItemId);
                return ResourceBLL.GetText(WebPartId, "DeleteOkMessage", "Xóa thành công");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetShowFormSetPermissionLawDocumentItem(string LawDocumentItemId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                LawDocumentItemCls LawDocumentItemCls = new LawDocumentBLL().CreateModelItem(LawDocumentItemId);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new LawDocument().PermissionTypes, "LawDocumentItems");

                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "PermissionItem", LawDocumentItemId, LawDocumentItemCls.Owner, true);
                return Environment.Permission.GetPermissionUrl(WebPartId, LawDocumentItemId, "LawDocumentItems");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string SyncLawDocumentItem(string LawDocumentItemId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                new LawDocumentBLL().SyncWithServer(LawDocumentItemId);
                return GetText("SycOk", "Sync với máy chủ internet thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetConfirmSyncAllMsg()
        {
            return ResourceBLL.GetText(WebPartId, "ConfirmSyncAllMsg", "Bạn đã chắc chắn update lại dữ liệu tin tức trên máy chủ từ máy local chưa?");
        }
    }
}